<template>
  <el-dialog :close-on-click-modal="false"   title="转为事件" :visible="show" :before-close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="事件类型" prop="eventLevel">
        <el-select v-model="form.category" placeholder="请选择" @change="onChangeEventType($event, 'quesOptions', problemOptions)">
          <el-option v-for="item in problemOptions" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="问题类型" :prop="isShowQues ? `type`:''">
        <el-select v-model="form.type" placeholder="请选择" style="width:100%;" @change="onChangeProblem($event, 'detailOptons', quesOptions)">
          <el-option v-for="item in quesOptions" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="详细类型" :prop="isShowQues?`subType`:''">
        <el-select v-model="form.subType" placeholder="请选择"  style="width:100%;" @change="onChangeProblem($event, null, detailOptons)">
          <el-option v-for="item in detailOptons" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="事件描述" prop="content">
        <el-input v-model="form.content" type="textarea" :autosize="{ minRows: 4, maxRows: 6 }" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" :autosize="{ minRows: 4, maxRows: 6 }" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="要求完成" prop="reqHandleTime">
        <el-input-number v-model="form.reqHandleTime" placeholder="请输入"></el-input-number>
        <el-select v-model="form.reqHandleTimeUnit" placeholder="请选择单位" style="margin-left: 10px;">
          <el-option
            v-for="item in [{label: '天', value: '天'},{label: '小时', value: '小时'},{label: '分钟', value: '分钟'}]"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getProblemTree } from "@/api/event";
import { subForm } from '@/api/flow'
export default {
  props: {
    /* 显示隐藏 */
    show: {
      type: Boolean,
      default: false
    },
    item: {
      type: Object,
      default: {}
    }
  },
  watch: {
    item: {
      handler(val) {
        // this.form = val || {reqHandleTimeUnit: '天'};
        this.$set(this.form, 'remark', val && val.content)
        this.$set(this.form, 'reqHandleTimeUnit', '天')
      },
      immediate: true
    },
    show: {
      handler(val) {
        if(val) {
            this.getProblemTree()
        }
        val && this.resetForm('form');
      },
      immediate: true
    }
  },
    computed: {
    isDisabled() {
      return this.isShowQues && this.form.problemType != "";
    },
    isShowQues() {
      const reg = /^JSWNJHJWSB[A-Z]*/;
      return reg.test(this.form.handleDeptCode);
      // return `JSWNJHJWSB`.includes(this.form.handleDeptCode)
    },
  },
  data() {
    return {
      form: {
        reqHandleTimeUnit: '天'
      },
      rules: {
        deptCode: [{ required: true, message: '请输入内容', trigger: ['change', 'input'] }],
        reqHandleTime: [{ required: true, message: '请选择完成时间和单位', trigger: ['change', 'input'] }]
      },
      problemOptions: [],
      quesOptions: [],
      detailOptons: []
    };
  },
  methods: {
    submit() {
      console.log('打印当前编辑：', this.form);
      this.$set(this.form, "ARG_DEFINE_CODE", 'work');
        this.$set(this.form, "ARG_DEFINE_START_NODE", 'start');
        this.$set(this.form, "ARG_USER_SYSTEM", 1);
        this.$set(this.form, "sourceType", 'DEVICE');
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          try {
            const res = await subForm(this.form)
            // 验证成功-编辑
            this.$emit('change', this.form);
            this.close();
          } catch (error) {
            console.log('预警转事件：', error)
          }
        }
      });
    },
    close() {
      this.form = {reqHandleTimeUnit: '天'};
      this.$emit('update:show', false);
    },
    async getProblemTree() {
      try {
        const res = await getProblemTree({ id: 0 });
        this.problemOptions = res.data;
      } catch (error) {}
    },
    onChangeEventType(e, optionField, optionsList) {
      const item = optionsList.find(i => i.id == e)
      this[optionField] = item.list;
      this.$set(this.form, "reqHandleTime", item.time);
      this.$set(this.form, "reqHandleTimeUnit", item.unit);
      this.$set(this.form, "content", item.desc);
      this.$set(this.form, "categoryName", item.name);
      this.$set(this.form, "type", '');
      this.$set(this.form, "typeName", '');
      this.$set(this.form, "subType", '');
      this.$set(this.form, "subTypeName", '');
    },
    onChangeProblem(e, optionField, optionsList) {
      const item = optionsList.find(i => i.id == e)
      if(optionField) {
        this[optionField] = item.list;
        this.$set(this.form, "typeName", item.name);
        this.$set(this.form, "subType", '');
        this.$set(this.form, "subTypeName", '');
      }else{
        this.$set(this.form, "subTypeName", item.name);
      }
      this.$set(this.form, "reqHandleTime", item.time);
      this.$set(this.form, "reqHandleTimeUnit", item.unit);
      this.$set(this.form, "content", item.desc);
    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
}
</style>
